﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 

public partial class management_Article_Category_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDrpClass();
        }
    }

    //绑定顶级分类
    private void BindDrpClass()
    {
        int total = 0;
        DataTable dt = BLL.Article_Category.Categorys_Get(0, int.MaxValue, 0, out total);
        dropCategory.Items.Clear();
        dropCategory.Items.Add(new ListItem("一级分类", "0"));
        DataRow[] drs = dt.Select("FId= " + 0);
        foreach (DataRow dr in drs)
        {
            string classid = dr["Id"].ToString();
            string classname = dr["Title"].ToString();
            //顶级分类显示形式
            classname = "╋" + classname;
            dropCategory.Items.Add(new ListItem(classname, classid));
            int sonparentid = int.Parse(classid);
            string blank = "├─";
            //递归子分类方法
            BindNode(sonparentid, dt, blank);
        }
        dropCategory.DataBind();
    }

    //绑定子分类
    private void BindNode(int parentid, DataTable dt, string blank)
    {
        DataRow[] drs = dt.Select("FId= " + parentid);
        foreach (DataRow dr in drs)
        {
            string classid = dr["Id"].ToString();
            string classname = dr["Title"].ToString();
            classname = blank + classname;
            dropCategory.Items.Add(new ListItem(classname, classid));
            int sonparentid = int.Parse(classid);
            string blank2 = blank + "─";
            BindNode(sonparentid, dt, blank2);
        }
    }

    //提交按钮事件
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        Model.Article_Category Info = new Model.Article_Category();
        Info.Title = txtTitle.Text;
        Info.FId = Convert.ToInt32(dropCategory.SelectedValue);
        Info.Shows = ckShows.Checked;
        Info.UpTime = DateTime.Now;
        try
        {
            BLL.Article_Category.Add(Info);
             Page.RegisterStartupScript("back", "<script   languge='javascript'>alert('添加成功！');</script>");
             txtTitle.Text = "";
             BindDrpClass(); ;
             ckShows.Checked = false;
        }
        catch (Exception)
        {

            Page.RegisterStartupScript("back", "<script   languge='javascript'>alert('添加失败！');</script>");
        }
        
    }

    //返回按钮事件
    protected void btn_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Category_List.aspx");
    }
}
